package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * @author げっしょく
 * @date 2021/10/21
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 发送验证码
     * @param map 手机号码
     * @return
     */
    @PostMapping("/login")
    public ResponseEntity login(@RequestBody Map map) {
        String phone = (String) map.get("phone");
        userService.sendMsg(phone);
        return ResponseEntity.ok(null);

    }

    /**
     * 校验验证码
     * @param map 手机号码和其用户输入的验证码
     * @return 返回的是该用户是否是新用户和token字符串
     */
    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map map) {
        String phone = (String) map.get("phone");
        String verificationCode = (String) map.get("verificationCode");
        Map login = userService.loginVerification(phone, verificationCode);
        //System.out.println(login.get("token"));
        return ResponseEntity.ok(login);
    }

    /**
     * 新用户保存的基础信息
     * @param userInfo  新用户需要保存的信息
     * @return
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo) {
        userInfo.setId(UserHolder.getId());
        userInfoService.save(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * 设置头像
     * @param headPhoto 图片路径
     * @return
     * @throws IOException
     */
    @PostMapping("/loginReginfo/head")
    public ResponseEntity sethead(MultipartFile headPhoto) throws IOException {
        userInfoService.saveFace(headPhoto);
        return ResponseEntity.ok(null);
    }







}
